{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "37e57229-e416-4476-ae6a-05441e50d5d7",
   "metadata": {},
   "source": [
    "## Practice 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f32ac401-6ae8-4ac5-8a39-38239ed0fd49",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af4b5921-33dc-4965-94a3-24f40c05f4a5",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### No.1\n",
    "\n",
    "给定一个 4 维数组，如何得到最后两维所有数据的和？（hint：指定 axis 进行计算）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "71019bc5-f2ec-4978-b386-084cd265745c",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 4, 5, 6)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.random.randint(0, 20, size = (3, 4, 5, 6))\n",
    "arr.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "66385d70-aa75-4f61-bdbb-a6f5d4708820",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 4)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[290, 291, 265, 280],\n",
       "       [255, 243, 256, 290],\n",
       "       [305, 307, 225, 286]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 最后两维求和\n",
    "res = arr.sum(axis = (-2, -1))\n",
    "display(res.shape)\n",
    "display(res)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10340299-5684-4c9c-a9a4-4df1a381359b",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### No.2\n",
    "\n",
    "给定数组 `[1,2,3,4,5]`，请在这个数组每个元素之间插入 3 个 0，并打印出新数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f0218e1a-ba33-4e08-bd20-65741a71fb60",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.arange(1,6)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "73b9c58b-3a1a-4dcd-8466-db9e1ddec4fa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=int16)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#17 = 5 + 4*3\n",
    "arr2 = np.zeros(shape=17, dtype=np.int16)\n",
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0737f917-2dee-4c79-81e0-20c416ae0ba5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5], dtype=int16)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2[::4] = arr\n",
    "arr2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a730e7b1-1555-4715-90c2-49bafe158556",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### No.3\n",
    "\n",
    "给定一个 2 维矩阵（5x4），如何交换其中两行的元素？（hint：花式索引）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c04a1f5d-008b-4a40-8f5d-b90484c6be57",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[15,  0,  8, 10, 15],\n",
       "       [14,  3, 12,  0, 19],\n",
       "       [ 2,  6,  4,  7,  0],\n",
       "       [ 2, 16, 12, 18, 19]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.random.randint(0, 20, size = (4,5))\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "589beed1-0359-4a04-a7dd-fafb252c4833",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2, 16, 12, 18, 19],\n",
       "       [14,  3, 12,  0, 19],\n",
       "       [ 2,  6,  4,  7,  0],\n",
       "       [15,  0,  8, 10, 15]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2 = arr[[3, 1, 2, 0]]\n",
    "arr2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d840453f-4356-427d-8c07-d3465ed9da64",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### No.4\n",
    "\n",
    "创建一个长度为 `100000` 的随机数组，使用两种方法对其求三次方（for循环；numpy方法），并统计所用方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a9a5ad13-fe37-436b-98a3-a4d3550c6ed5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 7, 4, ..., 0, 2, 4])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.random.randint(0, 10, size = 100000)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e080654d-b845-40cf-86e7-3595b9eac6e7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 2.54 ms, sys: 111 µs, total: 2.65 ms\n",
      "Wall time: 325 µs\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([ 64, 343,  64, ...,   0,   8,  64])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "res1 = np.power(arr, 3)\n",
    "res1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "44cfe9d6-5147-4449-a38b-022ce4713c1d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 23.6 ms, sys: 995 µs, total: 24.6 ms\n",
      "Wall time: 9.63 ms\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[64, 343, 64, 512]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "res2 = []\n",
    "for i in arr:\n",
    "    res2.append(i ** 3)\n",
    "res2[:4]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "773fdf93-9a92-46c0-8559-e2d0e6d93eee",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### No.5\n",
    "\n",
    "创建一个 5x3 的矩阵和一个 3x2 的矩阵，计算二者乘积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "15c102d2-eeac-436f-a61d-c6832e7b2cd7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[144,  72],\n",
       "       [146,  74],\n",
       "       [153,  77],\n",
       "       [ 59,  31],\n",
       "       [ 94,  46]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randint(0, 10, size=(5,3)) @ np.random.randint(0, 10, size=(3,2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81718a7c-4cfb-41c5-93c6-de7dccc128fb",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### No.6\n",
    "\n",
    "矩阵的每一行的元素都减去改行的平均值（hint：考虑指定 axis）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "10ad0e21-4f70-4ae7-9516-dded7df22b3a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8, 4, 8, 8],\n",
       "       [8, 4, 1, 1],\n",
       "       [6, 0, 2, 1]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.random.randint(0, 10, size = (3,4))\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "7312270f-dc52-4f65-8d3e-e6871419c9c9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([7.  , 3.5 , 2.25])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "line_avg = arr.mean(axis = 1)\n",
    "line_avg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d80c19a3-e73e-4ae9-a06f-538a37dd9479",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.  , -3.  ,  1.  ,  1.  ],\n",
       "       [ 4.5 ,  0.5 , -2.5 , -2.5 ],\n",
       "       [ 3.75, -2.25, -0.25, -1.25]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr - line_avg.reshape(3, 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "92b11cbe-0d3a-4b40-8dc6-18bd6891632e",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### No.7\n",
    "\n",
    "创建下边给出矩阵（开始时需要使用 `np.zeros` 创建 8x8 的矩阵）\n",
    "\n",
    "```\n",
    "[[0 1 0 1 0 1 0 1] \n",
    " [1 0 1 0 1 0 1 0]\n",
    " [0 1 0 1 0 1 0 1]\n",
    " [1 0 1 0 1 0 1 0]\n",
    " [0 1 0 1 0 1 0 1]\n",
    " [1 0 1 0 1 0 1 0]\n",
    " [0 1 0 1 0 1 0 1]\n",
    " [1 0 1 0 1 0 1 0]]\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "d3dc02c7-e415-4e18-8239-23a646765940",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0, 0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0, 0, 0, 0]], dtype=int16)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.zeros(shape = (8,8), dtype = np.int16)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "57bbb234-c63c-4964-b4e5-1edc99a77aae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 0, 1, 0, 1, 0, 1],\n",
       "       [0, 0, 0, 0, 0, 0, 0, 0],\n",
       "       [0, 1, 0, 1, 0, 1, 0, 1],\n",
       "       [0, 0, 0, 0, 0, 0, 0, 0],\n",
       "       [0, 1, 0, 1, 0, 1, 0, 1],\n",
       "       [0, 0, 0, 0, 0, 0, 0, 0],\n",
       "       [0, 1, 0, 1, 0, 1, 0, 1],\n",
       "       [0, 0, 0, 0, 0, 0, 0, 0]], dtype=int16)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2n 行\n",
    "arr[::2] = 1\n",
    "arr[:, ::2] = 0\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "eeebe90c-f890-4246-a8b6-786c777c6857",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 0, 1, 0, 1, 0, 1],\n",
       "       [1, 0, 1, 0, 1, 0, 1, 0],\n",
       "       [0, 1, 0, 1, 0, 1, 0, 1],\n",
       "       [1, 0, 1, 0, 1, 0, 1, 0],\n",
       "       [0, 1, 0, 1, 0, 1, 0, 1],\n",
       "       [1, 0, 1, 0, 1, 0, 1, 0],\n",
       "       [0, 1, 0, 1, 0, 1, 0, 1],\n",
       "       [1, 0, 1, 0, 1, 0, 1, 0]], dtype=int16)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2n+1 行\n",
    "arr[1::2] = 1\n",
    "arr[1::2, 1::2] = 0\n",
    "arr"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29c8a1e4-7531-4c30-ba13-ffabd2559747",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### No.8\n",
    "\n",
    "正则化一个 5x5 的随机矩阵（数据从一变成 0~1 之间的数字，相当大进行缩小）\n",
    "\n",
    "正则化：矩阵 A 中的每一列减去这一列最小值，除以每一列的最大值减去每一列的最小值（hint：指定 axis）\n",
    "\n",
    "<font size=6>$A = \\frac{A - A.min}{A.max - A.min}$</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "eb7a1150-50ff-4438-903d-159d1ba1feb5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8, 7, 8, 0, 2],\n",
       "       [2, 7, 3, 2, 7],\n",
       "       [7, 6, 5, 2, 0],\n",
       "       [0, 2, 4, 0, 9],\n",
       "       [6, 7, 9, 3, 9]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.random.randint(0, 10, size = (5,5))\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "09e6ac33-18bf-4952-9268-7e52e09136ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 2, 3, 0, 0])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([8, 7, 9, 3, 9])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "col_min = arr.min(axis = 0)\n",
    "col_max = arr.max(axis = 0)\n",
    "\n",
    "display(col_min, col_max)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "87a066b8-9148-46f2-9142-b543133d2e9a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8, 5, 5, 0, 2],\n",
       "       [2, 5, 0, 2, 7],\n",
       "       [7, 4, 2, 2, 0],\n",
       "       [0, 0, 1, 0, 9],\n",
       "       [6, 5, 6, 3, 9]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frac_up = arr - col_min\n",
    "frac_up"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "76cfe859-e4e8-4f42-a718-42b89c666745",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8, 5, 6, 3, 9])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frac_down = col_max - col_min\n",
    "frac_down"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "b89cbf73-50d0-401e-a9f5-2a187a64cce7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.        , 1.        , 0.83333333, 0.        , 0.22222222],\n",
       "       [0.25      , 1.        , 0.        , 0.66666667, 0.77777778],\n",
       "       [0.875     , 0.8       , 0.33333333, 0.66666667, 0.        ],\n",
       "       [0.        , 0.        , 0.16666667, 0.        , 1.        ],\n",
       "       [0.75      , 1.        , 1.        , 1.        , 1.        ]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frac_up / frac_down"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ebb1acc-0a14-42e2-a3f1-9b1bce309cdd",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### No.9\n",
    "\n",
    "根据两个或多个调价按过滤 numpy 数组：加载 iris 数据集，筛选出第一列小于 5.0 并且第三列大于 1.5 的数据（使用布尔类型的花式索引进行筛选）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "7e9e32a1-c1cf-4df5-b9c1-2fd52889f104",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(150, 4)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.loadtxt('iris_dataset.csv', delimiter=',')\n",
    "data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "66ca572c-93a5-452d-b9c4-fd08fa5c73ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "cond1 = data[:, 0] < 5\n",
    "cond2 = data[:, 2] > 1.5\n",
    "\n",
    "cond = cond1 & cond2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "02b468da-737e-4a32-94ea-582d446f6487",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4.8, 3.4, 1.6, 0.2],\n",
       "       [4.8, 3.4, 1.9, 0.2],\n",
       "       [4.7, 3.2, 1.6, 0.2],\n",
       "       [4.8, 3.1, 1.6, 0.2],\n",
       "       [4.9, 2.4, 3.3, 1. ],\n",
       "       [4.9, 2.5, 4.5, 1.7]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[cond]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca07457e-6504-4520-8947-a713862bd798",
   "metadata": {},
   "source": [
    "### No.10\n",
    "\n",
    "计算出 iris 数据集中每个数据在每一行的 sofrmax 得分（hint：exp 表示自然底数 e）\n",
    "\n",
    "<font size=6>$Softmax(z_i)=\\frac{exp(z_i)}{\\sum_j{exp(z_j)}}$</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "2cb04e08-589c-44f9-8e7c-ecfc3d09d616",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7.3890560989306495"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.e ** 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "2f4d5234-060b-445c-a6dd-715fac25ae11",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7.38905609893065"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.exp(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "539c1796-d7ab-46a6-9c2d-64d8566239ca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(150, 4)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.loadtxt('iris_dataset.csv', delimiter=',')\n",
    "data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "1234ae6d-61aa-45b3-b630-c0c29dc4893e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5.1, 3.5, 1.4, 0.2],\n",
       "       [4.9, 3. , 1.4, 0.2],\n",
       "       [4.7, 3.2, 1.3, 0.2]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "ecf2a437-560f-4c05-936b-a568753fda15",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[164.0219073 ,  33.11545196,   4.05519997,   1.22140276],\n",
       "       [134.28977968,  20.08553692,   4.05519997,   1.22140276],\n",
       "       [109.94717245,  24.5325302 ,   3.66929667,   1.22140276]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "exp_data = np.exp(data)\n",
    "exp_data[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "38c6f75b-4f56-4a20-92f7-76f70dc9bb98",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(150,)"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "row_exp_sum = exp_data.sum(axis = 1)\n",
    "row_exp_sum.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "7084fae3-0d8d-4ab1-9a33-a4ab9c65fdf1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.81032902, 0.16360261, 0.02003419, 0.00603418],\n",
       "       [0.84114103, 0.1258083 , 0.02540026, 0.00765041],\n",
       "       [0.78888466, 0.17602396, 0.02632766, 0.00876372],\n",
       "       [0.78097135, 0.17425826, 0.03518214, 0.00958825],\n",
       "       [0.77993968, 0.19233076, 0.02131086, 0.00641871]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res = exp_data / row_exp_sum.reshape(150, 1)\n",
    "res[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ceeb10b1-cdf7-4072-a488-381aef4a285d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
